import time
import uuid
import pymysql
from selenium import webdriver
from yangRongXu.selenium.组件分离.loctor import *
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.common.by import By
db = pymysql.connect(host="121.41.51.49",user="root",password="Aa010903!",database="crmeb1",cursorclass=pymysql.cursors.DictCursor)
cursor = db.cursor()


class CRMEB:
    uuid_ = str(uuid.uuid4()).split('-')[0]
    def __init__(self,driver,url):
        self.driver = driver
        # 初始化驱动
        self.driver = webdriver.Chrome()
        # 打开网页
        self.driver.get(url)
        # 设置隐式等待
        self.driver.implicitly_wait(15)
        # 最大化窗口
        self.driver.maximize_window()
    def login_(self,user:str,password:str):
        # 输入账号：
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(user_ele))[0].send_keys(user)
        # 输入密码：
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(password_ele))[1].send_keys(password)
        # 点击登录按钮
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(login_ele))[0].click()
    def add_user_group(self,name):
        self.name = name
        # 点击用户
        time.sleep(3)
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(user_icon_ele))[1].click()
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(user_group_ele))[1].click()
        # 点击添加
        time.sleep(3)
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_element_located(add_icon_ele)).click()
        # 输入名称
        time.sleep(3)
        WebDriverWait(self.driver,10,0.50).until(EC.presence_of_all_elements_located(input_user_group_ele))[2].send_keys(self.name+str(self.uuid_))
        # 点击确定
        time.sleep(1)
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(click_accept_ele))[2].click()
        time.sleep(2)
    def cherk_name(self):
        text = self.driver.find_element(By.XPATH, f"//*[text()='{self.name}{str(self.uuid_)}']").text
        assert text == self.name + str(self.uuid_)
        print(text)
        time.sleep(2)
    def del_user_group(self):
        # 在数据库表里面找到分组数目
        sql = "select count(*) from eb_user_group"
        cursor.execute(sql)
        result = cursor.fetchall()
        db.close()
        for i in result:
            for j in i.values():
                a = int(j) * 2 - 1
        time.sleep(3)
        # 点击删除
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(del_icon_ele))[a].click()
        # 点击确定
        time.sleep(2)
        WebDriverWait(self.driver,10,0.5).until(EC.presence_of_all_elements_located(click_accept1_ele))[2].click()
if __name__ == '__main__':

    driver = webdriver.Chrome()
    url = "http://121.41.51.49:8899/admin/"
    # 实例化对象
    c1 = CRMEB(driver,url)
    c1.login_("admin","123456")
    c1.add_user_group("小丁姐")
    c1.cherk_name()
    c1.del_user_group()
    c1.cherk_name()

    time.sleep(5)